﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using Janus.Windows.UI;
using System.IO;
using Janus.Windows.GridEX.EditControls;
using System.Management;
using System.Diagnostics;
using Janus.Windows.EditControls;
using VSSTQH.Properties;

namespace VSSTQH
{
    public partial class FrmConfiguration : Form
    {

        #region Singleton Members
        private static volatile FrmConfiguration instance;
        private static object syncRoot = new Object();

        public static FrmConfiguration Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new FrmConfiguration();
                }

                return instance;
            }
        }
        #endregion

        #region Field Members

        private readonly ParaPort _paraPort = new ParaPort();
        private readonly SettingApp _settings = new SettingApp();
        private readonly CompanyInfo _company = new CompanyInfo();

        #endregion

        #region Properties
        private int TaskType
        {
            get
            {
                int result = 0;
                if (rdoOnce.Checked)
                    result = 1;
                else if (rdoDaily.Checked)
                    result = 2;
                else if (rdoWeekly.Checked)
                    result = 3;
                else if (rdoMonthly.Checked)
                    result = 4;
                return result;
            }
            set
            {
                switch (value)
                {
                    case 0:
                        rdoNone.Checked = true;
                        break;
                    case 1:
                        rdoOnce.Checked = true;
                        break;
                    case 2:
                        rdoDaily.Checked = true;
                        break;
                    case 3:
                        rdoWeekly.Checked = true;
                        break;
                    case 4:
                        rdoMonthly.Checked = true;
                        break;
                }
            }
        }
        #endregion

        #region Ctors
        public FrmConfiguration()
        {
            InitializeComponent();

            Initialize();
        }

        private void Initialize()
        {

            #region Main process (Load, OK, Cancel)
            this.MaximizeBox = false;

            this.Load += new EventHandler(delegate
            {
                LoadPortControl();

                //port 1
                cboPortName.Text = _paraPort.PortName;
                cboBaudRate.SelectedValue = _paraPort.BaudRate;
                cboParity.SelectedIndex = _paraPort.Parity;
                cboDataBits.SelectedValue = _paraPort.DataBits;

                //port 2
                cboPortName2.Text = _paraPort.PortName2;
                cboBaudRate2.SelectedValue = _paraPort.BaudRate2;
                cboParity2.SelectedIndex = _paraPort.Parity2;
                cboDataBits2.SelectedValue = _paraPort.DataBits2;

                txtPrinterReceipt.Text = _settings.ReceiptPrintName;
                txtPrinterReport.Text = _settings.ReportPrintName;
                chkPreviewReceipt.Checked = _settings.IsPreviewReceiptPrint;
                chkPreviewReport.Checked = _settings.IsPreviewReportPrint;

                numMarginLeft.Value = _settings.MarginLeft;
                numMarginRight.Value = _settings.MarginRight;
                numMarginTop.Value = _settings.MarginTop;
                numMarginBottom.Value = _settings.MarginBottom;

                edtCompanyName.Text = _company.Name;
                edtAddress.Text = _company.Address;
               // string[] _text = _company.MM_S2C.Split(Path.DirectorySeparatorChar); 
                edtPath.Text = _company.MM_C2S.Substring(0,_company.MM_C2S.LastIndexOf(@"\") -6);
            });

            this.btnOK.Click += new EventHandler(delegate { ClickButtonOK(); });
            this.btnGetPath.Click += new EventHandler(btnGetPath_Click);
            this.btnCancel.Click += new EventHandler(delegate
            {
                this.Close();
            });
            #endregion

            #region Printer
            this.btnPrinterReceipt.Click += new EventHandler(delegate
            {
                PrintDialog printDialog1 = new PrintDialog();
                printDialog1.ShowDialog();
                txtPrinterReceipt.Text = printDialog1.PrinterSettings.PrinterName.ToString();
            });

            this.btnPrinterReport.Click += new EventHandler(delegate
            {
                PrintDialog printDialog1 = new PrintDialog();
                printDialog1.ShowDialog();
                txtPrinterReport.Text = printDialog1.PrinterSettings.PrinterName.ToString();
            });
            #endregion
        }

        

        #endregion

        #region Events

        #endregion

        #region Methods

        private void LoadPortControl()
        {
            // Ports.
            foreach (var str in SerialPort.GetPortNames())
            {
                this.cboPortName.Items.Add(str);
                this.cboPortName2.Items.Add(str);
            }

            // Parity.
            foreach (string str in Enum.GetNames(typeof(Parity)))
            {
                cboParity.Items.Add(str);
                cboParity2.Items.Add(str);
            }
        }
        #endregion

        #region Action Members

        /// <summary>
        /// Click button OK
        /// </summary>
        /// 
        void btnGetPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.Description = "Chọn thư mục lưu trữ mặc định.";
            folderDialog.RootFolder = Environment.SpecialFolder.MyComputer;
            folderDialog.ShowNewFolderButton = true;
            if (folderDialog.ShowDialog() == DialogResult.OK)
            {
               edtPath.Text = folderDialog.SelectedPath + @"\";
            }
        }

        private void ClickButtonOK()
        {
            try
            {
                _paraPort.PortName = cboPortName.Text;
                _paraPort.BaudRate = (int)cboBaudRate.SelectedValue;
                _paraPort.Parity = cboParity.SelectedIndex;
                _paraPort.DataBits = (int)cboDataBits.SelectedValue;

                _paraPort.PortName2 = cboPortName2.Text;
                _paraPort.BaudRate2 = Convert.ToInt32(cboBaudRate2.SelectedValue);
                _paraPort.Parity2 = cboParity2.SelectedIndex;
                _paraPort.DataBits2 = (int)cboDataBits2.SelectedValue;

                _settings.ReceiptPrintName = txtPrinterReceipt.Text;
                _settings.ReportPrintName = txtPrinterReport.Text;
                _settings.IsPreviewReceiptPrint = chkPreviewReceipt.Checked;
                _settings.IsPreviewReportPrint = chkPreviewReport.Checked;

                _settings.MarginLeft = (int) numMarginLeft.Value;
                _settings.MarginRight = (int)numMarginRight.Value;
                _settings.MarginTop = (int)numMarginTop.Value;
                _settings.MarginBottom = (int)numMarginBottom.Value;

                _company.Name = edtCompanyName.Text;
                _company.Address = edtAddress.Text;

                _company.MM_C2S = edtPath.Text + @"MM_C2S\";
                _company.MM_S2C = edtPath.Text + @"MM_S2C\";
                _company.SD_C2S = edtPath.Text + @"SD_C2S\";
                _company.SD_S2C = edtPath.Text + @"SD_S2C\";
                _company.Stored = edtPath.Text + @"LUUTRU\";

                Settings.Default.Save();

                Common.InitPort();

                Common.ShowSuccessful("Lưu cấu hình thành công!");
            }
            catch (Exception ex)
            {
                Common.ShowAlert("Lưu cấu hình thất bại => ERROR! " + ex.Message);
            }
        }

        protected override void OnClosed(EventArgs e)
        {
            instance = null;
            Settings.Default.Reload();
            base.OnClosed(e);
        }
        #endregion

    }

}
